<?php 
	function  build_subcategory(&$categories, $subs) {
		$output = '<ul>';
		foreach (explode(',', $subs) as $sub) {
			if ($categories[$sub]['type'] != 0) continue;
		    $output .= "<li class=\"" . (isset($categories[$sub]['sub']) ? 'haschild' : '') . "\"  id=\"cat{$sub}\">";
		    $output .= "<a href=\"{$categories[$sub]['link']}\"> - " . $categories[$sub]['name'] . (C('config_product_count') ? ' (' . $categories[$sub]['total'] . ')' : '') . "</a>";
		    if (isset($categories[$sub]['sub'])) {
		    	$output .= build_subcategory($categories, $categories[$sub]['sub']);
		    }
		    $output .= "</li>";
		}
		$output .= "</ul>";
		
		return $output;
	}

	if (IS_MOBILE) echo '<div class="sidebar">';
?>
<div class="box category">
  <div class="box-heading"><h3><span><?php echo $_['heading_title']; ?></span></h3></div>
  <div class="box-content">
    <ul class="box-category">
        <?php foreach ($categories['p'] as $cate_id) { ?>
		<?php if ($categories[$cate_id]['type'] != 0) continue; ?>
        <li class="<?php echo isset($categories[$cate_id]['sub']) ? 'haschild' : ''?>" id="cat<?php echo $cate_id;?>">
        <a href="<?php echo $categories[$cate_id]['link']; ?>"><?php echo $categories[$cate_id]['name'] . (C('config_product_count') ? ' (' . $categories[$cate_id]['total'] . ')' : ''); ?></a>    	      
		  <?php if (isset($categories[$cate_id]['sub'])) { ?>
		  <?php echo build_subcategory($categories, $categories[$cate_id]['sub']);?>
          <?php } ?>
        </li>
        <?php } ?>
      </ul>
 </div>
 </div>
 <script>
$(function(){
	var $category_id = <?php echo $category_id;?>;
	if ($category_id > 0) {
		$('#cat' + $category_id + ' > a').addClass('active');
		$('#cat' + $category_id).parents('li').find('a:first').addClass('active');
	}
});
 </script>
<?php if (IS_MOBILE) echo '</div>'; ?>